$(function () {
    load();
})

function fh() {
    localStorage.removeItem("mManufactureInsideAdd");
    location.href = "../../wen_zhi_yi/mManufactureInsideAdd.html";
}

function load() {
    let mManufacture = JSON.parse(localStorage.getItem("mManufactureInsideAdd"));
    $("#register").text(mManufacture.register);
    $("#registerTime").text(mManufacture.registertime);
    $("#checker").text(mManufacture.checker);
    $("#checkTime").text(mManufacture.checktime);
    $("#manufactureId").text(mManufacture.manufactureid);
    $("#productId").text(mManufacture.productid);
    $("#productName").text(mManufacture.productname);
    $("#amount").text(mManufacture.amount);
    $("#designer").text(mManufacture.designer);
    $("#gongshi").text(mManufacture.labourcostpricesum);
    $("#wuliao").text(mManufacture.modulecostpricesum);
    $("#realModuleCostPriceSum").text(mManufacture.realmodulecostpricesum == null ? '0.00' : mManufacture.realmodulecostpricesum);
    $("#realLabourCostPriceSum").text(mManufacture.reallabourcostpricesum == null ? '0.00' : mManufacture.reallabourcostpricesum);
    $("#productDescribe").text(mManufacture.productdescribe);
    $("#remark").text(mManufacture.remark);

    $.post("/Mmanufacture.do/mManufactureWZYSHlist", {id: mManufacture.id}, function (sdata) {
        let str = " <tr>\n" +
            "          <th>工序名称</th>\n" +
            "          <th>设计工时数</th>\n" +
            "          <th>实际工时数</th>\n" +
            "          <th>设计工时成本(元)</th>\n" +
            "          <th>实际工时成本</th>\n" +
            "          <th>设计物料成本(元)</th>\n" +
            "          <th>实际物料成本</th>\n" +
            "          <th>登记复核</th>\n" +
            "          <th></th>\n" +
            "      </tr>\n";
        $.each(sdata, function (index, item) {
            let as = JSON.stringify(item);
            let lS = JSON.stringify(sdata);
            let procedurefinishtag = item.procedurefinishtag == 'G004-0' ? "<a href='#' onclick='dj(" + as + "," + lS + ")'>登记</a>" :
                item.procedurefinishtag == 'G004-2' ? "<a href='#' >待复核</a>" :
                    item.procedurefinishtag == 'G004-1' ? '完成' : '';

            let proceduretransfertag = item.procedurefinishtag != 'G004-1' ? '' : item.proceduretransfertag == 'G005-0' ?
                "<a href='#' onclick='JJ(" + as + ")'>交接登记</a>" : item.proceduretransfertag == 'G005-2' ? '待复核' : '完成';
            str += "      <tr>\n" +
                "          <td>" + item.procedurename + "</td>\n" +
                "          <td>" + item.labourhouramount + "</td>\n" +
                "          <td>" + (item.reallabourhouramount == null ? '0.00' : item.reallabourhouramount) + "</td>\n" +
                "          <td>" + item.subtotal + "</td>\n" +
                "          <td>" + (item.realsubtotal == null ? '0.00' : item.realsubtotal) + "</td>\n" +
                "          <td>" + item.modulesubtotal + "</td>\n" +
                "          <td>" + (item.realmodulesubtotal == null ? '0.00' : item.realmodulesubtotal) + "</td>\n" +
                "          <td>" + procedurefinishtag + "</td>\n" +
                "          <td>" + proceduretransfertag + "</td>\n" +
                "      </tr>";
        })
        $("#tableCha").html(str);
    })
}

function dj(op, list) {
    let ls= [];
    let s= 0;
    for (let i=0;i<list.length;i++){
         if (list[i].procedurefinishtag!='G004-1' || list[i].proceduretransfertag!='G005-1'){
             ls[s]=list[i];
             s+=1;
         }
    }
    let zt = false;
    for (let j=0;j<ls.length;j++){
        if (ls[j].detailsnumber==(op.detailsnumber-1)){
            zt = true;
        }
    }
    if (zt){
        alert("上个工序完成才能进行登记！")
    }else {
        localStorage.setItem("mManufactureInsideAdd(1)",JSON.stringify(op));
        location.href="/wen_zhi_yi/mManufactureInsideAdd(1)(01).html";
    }
}


function JJ(op) {
    $("#AddJJModal").modal("show");
    sessionStorage.setItem("opid",JSON.stringify(op));
}

function qr() {
    let mManufacture = JSON.parse(localStorage.getItem("mManufactureInsideAdd"));
    let opid = JSON.parse(sessionStorage.getItem("opid"));
    let testedamount = $("#testedamount").val();

    console.log(testedamount)
    console.log(mManufacture.amount)

    if (testedamount==""){
        alert("请填写本次工序合格产品数量！")
        $("#AddJJModal").modal("hide");
        $("#testedamount").val(null);
        return;
    }
    if (parseInt(testedamount)<0){
        alert("本次工序合格产品数量不能小于0")
        $("#AddJJModal").modal("hide");
        $("#testedamount").val(null);
        return;
    }
    if (parseInt(testedamount)>mManufacture.amount){
        alert("本次工序合格产品数量不能大于投产数量！")
        $("#AddJJModal").modal("hide");
        $("#testedamount").val(null);
    }else {
        let param={testedamount:parseInt(testedamount),mmfid:mManufacture.id,mid:opid.id};
        $.post("/Mmanufacture.do/MmanufactureDengJiAddJJ", param, function (data) {
            $("#AddJJModal").modal("hide");
            $("#testedamount").val(null);
            location.href="/wen_zhi_yi/mManufactureInsideAdd(1).html";
        })
    }
}

